package com.wl.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wl.mapper.PersonalMapper;
import com.wl.pojo.*;
import com.wl.pojo.Personal.Collect;
import com.wl.pojo.Personal.Personal;
import com.wl.pojo.Personal.PersonalUser;
import com.wl.service.PersonalService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
public class PersonalServiceImpl implements PersonalService {

    @Autowired
    private PersonalMapper personalMapper;

    /**
     * 获取用户的基本信息，包括兴趣标签
     */
    @Override
    public Personal getPersonalInfo(Integer userId) {
        // 先获取到基本的信息
        Personal personal = personalMapper.getPersonalInfo(userId);
        // 获取到标签信息
        List<String> tags = personalMapper.getUserTags(userId);
        // 组装标签信息
        personal.setUserTags(tags);
        // 文章被浏览量
        personal.setPostPageViews(personalMapper.getPostPageViews(userId));
        return personal;
    }

    @Override
    public PageBean getFollowOrFans(Integer currentSwitch, Integer userId, Integer currentPage, Integer pageSize) {
        // 设置分页参数
        PageHelper.startPage(currentPage, pageSize);
        // 执行查询
        List<Personal> personalList;
        if(currentSwitch == 1) {
            // 查询关注
            personalList = personalMapper.getFollow(userId);
        } else {
            personalList = personalMapper.getFans(userId);
        }
        Page<Personal> p = (Page<Personal>) personalList;
        return new PageBean(p.getTotal(),p.getResult());
    }

    /**
     * 获取当前登陆用户的关注用户的id列表
     */
    @Override
    public List<Integer> getLoginUserFollow(Integer userId) {
        return personalMapper.getLoginUserFollow(userId);
    }

    /**
     * 获取个人主页页面的展示的文章内容
     */
    @Override
    public PageBean getPersonalPost(String activeName, Integer userId, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<Post> postList = personalMapper.getPersonalPost(activeName, userId);
        for (Post post : postList) {
            post.setTagNames(personalMapper.getPostTagNames(post.getPostId()));
            post.setTagIds(personalMapper.getPostTagIds(post.getPostId()));
        }
        Page<Post> p = (Page<Post>) postList;
        return new PageBean(p.getTotal(),p.getResult());
    }

    /**
     * 删除文章
     */
    @Override
    @Transactional
    public void deletePostById(Integer postId) {
        // 删除文章点赞表中的信息
        personalMapper.deletePostLikesByPostId(postId);
        // 删除文章收藏表中的信息
        personalMapper.deletePostCollectByPostId(postId);
        // 删除评论的点赞信息
        personalMapper.deleteCommentLikesByPostId(postId);
        // 删除文章的评论
        personalMapper.deleteCommentByPostId(postId);
        // 删除文章
        personalMapper.deletePostById(postId);
    }

    /**
     * 获取用户可供选择的兴趣标签数据
     */
    @Override
    public List<InterestTag> getInterest() {
        return personalMapper.getInterest();
    }

    /**
     * 获取用户信息
     */
    @Override
    public PersonalUser getUserInfo(Integer userId) {
        // 查询基本信息
        PersonalUser personalUser = personalMapper.getUserInfo(userId);
        // 查询标签信息
        personalUser.setUserTags(personalMapper.getUserTagsData(userId));
        return personalUser;
    }

    /**
     * 修改用户信息，仅包括用户名，个人介绍。兴趣标签
     */
    @Override
    @Transactional
    public void updateUserInfo(PersonalUser personalUser) {
        // 删除用户兴趣标签数据
        personalMapper.deleteUserTagsByUserId(personalUser.getUserId());
        // 重新更新用户标签数据
        personalMapper.setUserTags(personalUser.getUserId(), personalUser.getInterestIds());
        // 更新基本信息
        personalMapper.updateUserInfo(personalUser);
    }

    /**
     * 修改密码接口
     */
    @Override
    public void updatePassword(Integer userId, String password) {
        personalMapper.updatePassword(userId, password);
    }

    /**
     *
     * @param userId 用户ID
     * @return 返回 所有收藏夹以及收藏夹中收藏的内容
     */
    @Override
    public Object getCollectData(Integer userId) {
        // 获取收藏夹名、更新时间、内容数量
        List<Collect> collects = personalMapper.getCollectData(userId);
        // 获取每个收藏夹收藏的内容
        for(Collect collect : collects) {
            collect.setContentData(personalMapper.getCollectContentData(collect.getCategoryId()));
        }
        return collects;
    }

    /**
     * 删除收藏夹
     * @param categoryId 删除的收藏夹ID
     */
    @Override
    @Transactional
    public void deleteCategory(Integer categoryId) {
        // 删除收藏夹中文章
        personalMapper.deleteCollectByCategoryId(categoryId);
        // 删除收藏夹
        personalMapper.deleteCategoryById(categoryId);
    }

    /**
     * 更新收藏夹
     */
    @Override
    public void updateCategory(Collect collect) {
        personalMapper.updateCollectCategory(collect);
    }
}
